home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TSPA3370.ZIP / SELFTEST.PAS < prev    next >
Pascal/Delphi Source File  |  1993-08-16  |  4KB  |  103 lines

  1. (*
  2.      Programmers: Help fighting viruses and patching.
  3.  
  4. Viruses and unauthorized patching are problems which should be
  5. fought against by the PC community. This program demonstrates a
  6. simple and a reasonably general, fast selftest to detect whether the
  7. program has caught a virus, or if it has been amateurishly patched.
  8. The code is easily incorporated in any Turbo Pascal source code. The
  9. idea is to check whether the file date and size have been altered.
  10. Most .exe viruses work by appending their code to the .exe file
  11. altering the file size. Trivial patching changes the file date.
  12. Either of these is detected by selftest.
  13. ..................................................................
  14. Prof. Timo Salmi      Co-moderator of comp.archives.msdos.announce
  15. Moderating  at  garbo.uwasa.fi anonymous FTP archives 128.214.87.1
  16. Faculty of Accounting & Industrial Management; University of Vaasa
  17. Internet:  ts@uwasa.fi  Bitnet:  salmi@finfun; FI-65101,   Finland
  18. *)
  19.  
  20. program SelftestDemo;
  21.  
  22. uses Dos
  23.      {$IFDEF VER40}  (* To provide what TP 4.0 is missing *)
  24.      ,TSUNT45        (* as compared to the later TP versions *)
  25.      {$ENDIF}
  26.      ;
  27.  
  28. (* Define a datatype for the required information *)
  29. type SelftestRecordType = record
  30.                              size  : longint;
  31.                              year  : word;
  32.                              month : word;
  33.                              day   : word;
  34.                              ok    : boolean;
  35.                            end;
  36.  
  37. (* Define a selftest constant. Give initial values to match your
  38.    own program *)
  39. const SelftestRecord
  40.         : selftestRecordType
  41.         = (size  : 3648;
  42.            year  : 1992;
  43.            month : 11;
  44.            day   : 8;
  45.            ok    : true);
  46.  
  47. (* Tests whether file size and / or filedate have been changed.
  48.    Writes a warning message *)
  49. procedure SELFTEST (var selftestRecord : selftestRecordType);
  50. var FileInfo : SearchRec;
  51.     FileDate : DateTime;
  52.     oksize   : boolean;
  53.     okdate   : boolean;
  54.     progname : string;
  55. begin
  56.   oksize := true;
  57.   okdate := true;
  58.   {$IFDEF VER40} progname := ParamStr0;
  59.   {$ELSE}        progname := ParamStr(0);
  60.   {$ENDIF}
  61.   FindFirst (progname, AnyFile, FileInfo);
  62.   if DosError <> 0 then selftestRecord.ok := false;
  63.   if selftestRecord.ok then
  64.     if (FileInfo.Attr and VolumeId = 0) and
  65.        (FileInfo.Attr and Directory = 0) then
  66.       begin
  67.         if FileInfo.Size <> selftestRecord.size then
  68.           oksize := false;
  69.         UnpackTime (FileInfo.Time, FileDate);
  70.         if FileDate.year  <> selftestRecord.year then
  71.           okdate := false;
  72.         if FileDate.month <> selftestRecord.month then
  73.           okdate := false;
  74.         if FileDate.day   <> selftestRecord.day then
  75.           okdate := false;
  76.         selftestRecord.ok := oksize and okdate;
  77.       end;
  78.   if not selftestRecord.ok then
  79.     begin
  80.       writeln (#7, 'Warning for a patched or detached program, or a potential virus');
  81.       if not oksize then
  82.         writeln (progname, ' filesize has been altered');
  83.       if not okdate then
  84.         writeln (progname, ' filedate has been altered');
  85.     end;
  86. end;  (* selftest *)
  87.  
  88. procedure LOGO;
  89. begin
  90.   writeln;
  91.   writeln ('SELFTEST demo by Prof. Timo Salmi, 8-Nov-92');
  92.   writeln ('University of Vaasa, Finland, ts@uwasa.fi');
  93.   writeln;
  94. end;  (* logo *)
  95.  
  96. (* Main program *)
  97. begin
  98.   LOGO;
  99.   SELFTEST (selftestRecord);
  100.   if not selftestRecord.ok then halt;
  101.   writeln ('Hello world and whatever');
  102. end.  (* selftestDemo *)
  103.